//
//  YAProgressView.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/16.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit

class YAProgressView: UIImageView {
    //接收进度值
    var progress:CGFloat = 0 {
        didSet{
            progressView.progress = progress
        }
    }
    private lazy var progressView:YAProgressInnerView = {
        let p = YAProgressInnerView()
        p.backgroundColor = UIColor.clearColor()
        self.addSubview(p)
        printLog(p.frame)
        return p
    }()
    override func layoutSubviews() {
        super.layoutSubviews()
        progressView.frame = bounds
    }
    //类中类，专供本类使用，应为ImageView本身不能自己绘制
    private class YAProgressInnerView:UIView{
        var progress:CGFloat = 0 {
            didSet{
                setNeedsDisplay()
            }
        }
        private override func drawRect(rect:CGRect){
            if progress>=1 {
                //self = nil
                return
            }

            //绘制曲线
            let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
            let r = min(rect.width,rect.height)*0.5
            let start = -CGFloat(M_PI_2)
            let end = 2*CGFloat(M_PI)*progress + start
            
            let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
            //增加指向圆心的路径
            path.addLineToPoint(center)
            //关闭路径，产生一个扇形
            path.closePath()
            //设置属性 在实际中，动画只是点缀，不要抢
            
            UIColor(white: 0.0, alpha: 0.5).setFill()
            path.fill()
        }
    }
}
